package com.base.pig.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.base.pig.dto.CaseBindDto;
import com.base.pig.dto.CaseListDto;
import com.base.pig.dto.IdDto;
import com.base.pig.entity.WCase;
import com.base.pig.service.WCaseService;

import com.base.pig.util.R;
import com.base.pig.vo.CaseListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 *  巡检事件controller
 * </p>
 *
 * @author wangzhx
 * @since 2022-03-21
 */
@RestController
@Api(value = "WCaseController", tags = "巡检事件管理")
@RequestMapping("/WCaseController")
public class WCaseController {

    @Autowired
    public WCaseService wCaseService;

    @ApiOperation(value = "新增巡检事件")
    @PostMapping("/save")
    public R save(@RequestBody WCase wCase){
        Date date = new Date();
        wCase.setTime(date);
        wCase.setStatus("未处理");
        boolean b = wCaseService.save(wCase);
        if (b){
            return R.ok(b,"添加成功");
        }
        return R.failed(null,"添加失败");
    }

    @ApiOperation(value = "根据id删除")
    @PostMapping("/delete")
    public R delete(@RequestBody IdDto idDto){
        wCaseService.removeById(idDto.getId());
        return R.ok(null,"删除成功");
    }


    /**
     * @param caseListDto: 请求参数集合 : {
     *             personid : 巡检人员id
     *             status : 状态
     *             current : 分页数,默认1
     *             size: 每页记录数
     *             }
     * @return R
     * @Title 分页获取巡检事件列表
     * @Description
     * @author wzx
     * @date 2022/3/21
     */
    @ApiOperation(value = "分页获取巡检事件列表")
    @PostMapping("/list")
    public R list(@RequestBody CaseListDto caseListDto){
        IPage<CaseListVo>  page = wCaseService.queryCaseList(caseListDto);
        return R.ok(page,"分页查询信息成功");
    }

    @ApiOperation(value = "根据id修改")
    @PostMapping("/update")
    public R update(@RequestBody WCase wCase){
        if(wCase.getId()!=null)
        {
            long count = wCaseService.count(Wrappers.<WCase>query().lambda().eq(WCase::getId,wCase.getId()));
            if(count != 0) {
                 return R.ok( wCaseService.updateById(wCase),"修改成功");
            }
            return R.failed(null,"未找到该数据");
        }
        return R.failed(null,"id不能为空");
    }

    @ApiOperation(value = "巡检事件绑定案件编号")
    @PostMapping("/bindCasenum")
    public R bindCasenum(@RequestBody CaseBindDto caseBindDto){
        if(caseBindDto.getId()!=null)
        {
            long count = wCaseService.count(Wrappers.<WCase>query().lambda().eq(WCase::getId,caseBindDto.getId()));
            if(count != 0) {
                WCase wcase = wCaseService.getById(caseBindDto.getId());
                wcase.setCasenum(caseBindDto.getCasenum());
                wcase.setStatus("已处理");
                return R.ok( wCaseService.updateById(wcase),"绑定成功");
            }
            return R.failed(null,"未找到该数据");
        }
        return R.failed(null,"id不能为空");
    }


}
